package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ManageShopSelfCatDeleteDTO;
import com.yuanfeng.business.dto.ManageShopSelfCatQueryDTO;
import com.yuanfeng.business.entity.ShopSelfCatEntity;
import com.yuanfeng.business.mapper.ShopSelfCatMapper;
import com.yuanfeng.business.service.ShopSelfCatService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.constant.CommonStatusEnum;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.shop.ManageShopSelfCatMenuVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ShopSelfCatServiceImpl
 * @Description 店铺商品分类
 * @Author cf
 * @Date 2023/6/27 13:45
 * @Version 1.0
 */
@Service
public class ShopSelfCatServiceImpl extends ServiceImpl<ShopSelfCatMapper, ShopSelfCatEntity> implements ShopSelfCatService {

    @Autowired
    private ShopSelfCatMapper shopSelfCatMapper;

    // 查询店铺自定义商品分类
    @Override
    public ResponseResult queryShopSelfCatList(UserTokenBeanDTO param) {

        List<ManageShopSelfCatMenuVO> menuList = new ArrayList<ManageShopSelfCatMenuVO>();
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService.judgePermissions(PermissionsConstant.SERLLER_GOODS,
                param.getToken());

        if (userPermissionsResult.getFlag()) {
            // 获取所有的分类
            List<ManageShopSelfCatMenuVO> rootMenus = shopSelfCatMapper.selectByShopId(Integer.valueOf(userPermissionsResult.getShopId()));
            // 获取多级分类
            menuList = getMenuList(rootMenus);
            if(null != menuList ){
                //menuList = menuList.stream().filter(x -> (x.getChildren() != null && x.getChildren().size() > 0)).collect(Collectors.toList());
            }
        } else {
            return ResponseResult.fail(CommonStatusEnum.FAIL.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(menuList);
    }

    /**
     * 找出一级菜单和二级菜单
     *
     * @param rootMenus
     * @return
     */
    public List<ManageShopSelfCatMenuVO> getMenuList(List<ManageShopSelfCatMenuVO> rootMenus) {
        List<ManageShopSelfCatMenuVO> list = new ArrayList<ManageShopSelfCatMenuVO>();
        // 先找到所有的一级菜单
        for (int i = 0; i < rootMenus.size(); i++) {
            ManageShopSelfCatMenuVO menu = rootMenus.get(i);
            // 一级菜单 没有父节点
            if (menu.getParentId() == 0) {
                list.add(menu);
            }
        }

        // 查找二级菜单
        /**
         * 利用递归找出所有子菜单
         */
        for (ManageShopSelfCatMenuVO menu : list) {
            menu.setChildren(getChild(menu.getShopGoodsCatId(), rootMenus));
        }

        return list;

    }

    /**
     * 找出子菜单
     *
     * @param parentId
     * @param rootMenu
     * @return
     */
    private List<ManageShopSelfCatMenuVO> getChild(Integer parentId, List<ManageShopSelfCatMenuVO> rootMenu) {
        // 子菜单
        List<ManageShopSelfCatMenuVO> childList = new ArrayList<ManageShopSelfCatMenuVO>();

        for (ManageShopSelfCatMenuVO menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (!StringUtils.isEmpty(menu.getParentId())) {
                if (menu.getParentId().equals(parentId)) {
                    childList.add(menu);
                }
            }
        }

        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }

        // 把子菜单的子菜单再循环一遍
        for (ManageShopSelfCatMenuVO menu : childList) {
            // 递归
            menu.setChildren(getChild(menu.getShopGoodsCatId(), rootMenu));
        }

        return childList;
    }

    @Override
    public ResponseResult deleteShopSelfCat(ManageShopSelfCatDeleteDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (null == userInfo) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        shopSelfCatMapper.deleteBatch(param.getShopGoodsCatIds());
        //删除子分类
        shopSelfCatMapper.deleteZiBatch(param.getShopGoodsCatIds());

        return ResponseResult.success();
    }

    @Override
    public ResponseResult queryShopSelfCat(ManageShopSelfCatQueryDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (null == userInfo) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        ManageShopSelfCatMenuVO menu = shopSelfCatMapper.selectByPrimaryKey(param.getShopGoodsCatId());
        return ResponseResult.success(menu);
    }
}
